package top.fengyulou.xiaofeng.controller.admin;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import top.fengyulou.xiaofeng.common.Jr;
import top.fengyulou.xiaofeng.common.MyUtils;
import top.fengyulou.xiaofeng.common.UserUtils;
import top.fengyulou.xiaofeng.domain.User;
import top.fengyulou.xiaofeng.dto.UserDto;
import top.fengyulou.xiaofeng.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 后台用户管理
 */
@Controller
public class AdminUserController {

    @Resource
    private UserService userService;
    @Value("${sys.operate.close}")
    private Integer sysOperateClose;

    /**
     * 列表页面
     */
    @RequestMapping("/admin/user/list")
    public String list() {
        System.out.println(sysOperateClose);
        return "admin/user/admin-user-list";
    }

    //保存页面
    @RequestMapping("/admin/user/save")
    public String savePage(String id, Model model) {
        if (StringUtils.isNotBlank(id)) {
            UserDto userDto = userService.getById(id);
            model.addAttribute("data", userDto);
        }
        return "admin/user/admin-user-save";
    }

    //修改密码页面
    @RequestMapping("/admin/user/update/pwd/{id}")
    public String updatePwd(@PathVariable String id,Model model) {
        model.addAttribute("id",id);
        return "admin/user/admin-user-update-pwd";
    }

    /**
     * 保存页面
     */
    @RequestMapping("/admin/user/excel/import")
    public String excelImport() {
        return "admin/user/admin-user-excel-import";
    }

    /**
     * 重置密码
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/admin/user/reset/password/{id}")
    public String resetPassword(@PathVariable String id, Model model) {
        model.addAttribute("id", id);
        return "admin/user/admin-user-reset-password";
    }

    //列表
    @RequestMapping("/admin/user/ajax/list")
    @ResponseBody
    public Jr listPage(HttpServletRequest request, @RequestBody UserDto userDto) {
        userDto.setCreateUserId(UserUtils.getUserId(request));
        userService.list(userDto);
        return Jr.yesd(userDto);
    }

    //保存
    @RequestMapping("/admin/user/ajax/save")
    @ResponseBody
    public Jr save(HttpServletRequest request, User user) {
        user.setCurrentUserId(UserUtils.getUserId(request));
        return userService.save(user);
    }

    //删除
    @RequestMapping("/admin/user/ajax/delete")
    @ResponseBody
    public Jr delete(HttpServletRequest request,@RequestParam String[] ids) {
        userService.delete(ids);
        return Jr.yes();
    }

    //修改密码
    @RequestMapping("/admin/user/ajax/update/pwd")
    @ResponseBody
    public Jr updatePwd(HttpServletRequest request,@RequestParam String id,@RequestParam String password) {
        User user = new User();
        user.setId(id);
        user.setPassword(MyUtils.getMd5(password));
        user.setCurrentUserId(UserUtils.getUserId(request));
        userService.update(user);
        return Jr.yes();
    }
}
